package com.hzw.saas.api.rbac.enums;

import java.util.Objects;

import cn.hutool.core.lang.Assert;

/**
 * 权限系统
 *
 * @author zzl
 * @since 08/30/2021
 */
public enum PermSysEnum {

    ADMIN(1, "后台管理用户"),
    USER(2, "云平台用户"),
    ;

    private final Integer code;
    private final String text;

    PermSysEnum(Integer code, String text) {
        this.code = code;
        this.text = text;
    }

    public Integer getCode() {
        return code;
    }

    public String getText() {
        return text;
    }

    public static String getText(Integer code) {
        PermSysEnum instance = instance(code);
        return Objects.isNull(instance) ? "" : instance.getText();
    }

    public static void assertValid(Integer code) {
        PermSysEnum instance = instance(code);
        Assert.notNull(instance, "权限所属系统未找到：{}", code);
    }

    public static PermSysEnum instance(Integer code) {
        for (PermSysEnum value : PermSysEnum.values()) {
            if (Objects.equals(value.getCode(), code)) {
                return value;
            }
        }
        return null;
    }

    public static PermSysEnum assertInstance(Integer code) {
        PermSysEnum instance = instance(code);
        Assert.notNull(instance, "权限所属系统未找到: {}", code);
        return instance;
    }
}
